﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ComputedTable.Version10
{
    public class ColumnResolver<TColumnId> : IColumnResolver<TColumnId>
    {
        private Dictionary<TColumnId, int> dictionary = new Dictionary<TColumnId, int>();

        public ColumnResolver()
        {
            if (!typeof(TColumnId).IsEnum)
            {
                throw new InvalidOperationException("TColumnId must be an Enum");
            }

            this.Ids = ((TColumnId[])Enum.GetValues(typeof(TColumnId))).ToList();
            foreach (var item in this.Ids)
            {
                this.dictionary[item] = Convert.ToInt32(item);
            }
        }

        public ColumnResolver(IList<TColumnId> columns)
        {
            this.Ids = columns;

            for(int i = 0; i < this.Ids.Count; i++)
            {
                var column = this.Ids[i];
                this.dictionary[column] = i;
            }
        }

        public IList<TColumnId> Ids { get; private set; }

        public int Count { get { return this.Ids.Count; } }
        
        public int IndexOf(TColumnId columnId)
        {
            return this.dictionary[columnId];
        }
    }
}
